package com.yinhe.music.yhmusic.autolisten;

import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.view.View;

import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceActivity;
import com.yinhe.music.yhmusic.cache.CacheManager;
import com.yinhe.music.yhmusic.model.LangType;
import com.yinhe.music.yhmusic.model.Music;
import com.yinhe.music.yhmusic.widget.CircleTextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Johnson on 2018/1/23.
 */

public class AutoListenActivity extends BaseServiceActivity implements View.OnClickListener, AutoListenContract.IAutoListenView {

    private List<CircleTextView> mCircleViewList = new ArrayList<>();

    private List<LangType> mTypeList;
    private int mTypeIndex = 0;

    AutoListenPresenter mPresenter;

    @Override
    public void initView() {
        mCircleViewList.add(findViewById(R.id.circle_1));
        mCircleViewList.add(findViewById(R.id.circle_2));
        mCircleViewList.add(findViewById(R.id.circle_3));
        mCircleViewList.add(findViewById(R.id.circle_4));
        mCircleViewList.add(findViewById(R.id.circle_5));
        mCircleViewList.add(findViewById(R.id.circle_6));
        mCircleViewList.add(findViewById(R.id.circle_7));
        mCircleViewList.add(findViewById(R.id.circle_8));
        mCircleViewList.add(findViewById(R.id.circle_9));
        mCircleViewList.add(findViewById(R.id.circle_10));
        mCircleViewList.add(findViewById(R.id.circle_11));
        mCircleViewList.add(findViewById(R.id.circle_12));

        findViewById(R.id.circle_1).setOnClickListener(this);
        findViewById(R.id.circle_2).setOnClickListener(this);
        findViewById(R.id.circle_3).setOnClickListener(this);
        findViewById(R.id.circle_4).setOnClickListener(this);
        findViewById(R.id.circle_5).setOnClickListener(this);
        findViewById(R.id.circle_6).setOnClickListener(this);
        findViewById(R.id.circle_7).setOnClickListener(this);
        findViewById(R.id.circle_8).setOnClickListener(this);
        findViewById(R.id.circle_9).setOnClickListener(this);
        findViewById(R.id.circle_10).setOnClickListener(this);
        findViewById(R.id.circle_11).setOnClickListener(this);
        findViewById(R.id.circle_12).setOnClickListener(this);
        findViewById(R.id.change_tv).setOnClickListener(this);

        showQuickControl(true);

        String title = CacheManager.getInstance().getLangName();
        if (TextUtils.isEmpty(title)) {
            title = "随心听";
        } else {
            title = "轻松一点，" + title + "随心听";
        }
        setToolbar(title);

        mPresenter.getSongClassify();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_auto_listen;
    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void initPresenter() {
        mPresenter = new AutoListenPresenter();
        mPresenter.attachModelView(new BaseModel(), this);
    }


    @Override
    protected void onDestroy() {
        mCircleViewList.clear();
        super.onDestroy();
    }

    @Override
    public void setSongClassifyUI(List<LangType> langRespList) {
        mTypeList = langRespList;
        updateData();
    }

    @Override
    public void setSongListUI(List<Music> musicList) {
        playSongs(musicList);
    }

    private void updateData() {
        if (mTypeList == null || mTypeList.size() == 0) {
            return;
        }

        int count = 12;
        if (mTypeList.size() < 12) {
            count = mTypeList.size();
            findViewById(R.id.change_tv).setVisibility(View.INVISIBLE);
        }

        for (int i = 0; i < count; i++) {
            LangType type = mTypeList.get((mTypeIndex++) % mTypeList.size());
            CircleTextView circleTextView = mCircleViewList.get(i);
            circleTextView.setText(type.songClassifyName);
            circleTextView.setMaxLines(2);
            circleTextView.setTag(type);
            circleTextView.setVisibility(View.VISIBLE);
        }

        for (int i = count; i < 12; i++) {
            mCircleViewList.get(i).setVisibility(View.GONE);
        }
    }


    private void sendSongRequest(LangType type) {
        mPresenter.getClassifySongs(type.songClassifyId);
    }

    private void playSongs(List<Music> songs) {
        if (songs != null && songs.size() > 0) {
            mService.playAll(songs);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.circle_1:
            case R.id.circle_2:
            case R.id.circle_3:
            case R.id.circle_4:
            case R.id.circle_5:
            case R.id.circle_6:
            case R.id.circle_7:
            case R.id.circle_8:
            case R.id.circle_9:
            case R.id.circle_10:
            case R.id.circle_11:
            case R.id.circle_12:
                LangType type = (LangType) view.getTag();
                if (type != null && type instanceof LangType) {
                    sendSongRequest(type);
                }
                break;
            case R.id.change_tv:
                updateData();
                break;
            default:
                break;
        }
    }
}
